//你好
eval(`console.log("hello from js.")`);
console.info("你好");

function format(num) {
  let [intPart, decPart] = num.toString().split('.');

  let result = '';
  let count = 0;

  for (let i = intPart.length - 1; i >= 0; i--) {
    result = intPart[i] + result;
    count++;
    if (count % 3 === 0 && i !== 0) {
      result = ',' + result;
    }
  }
  return decPart ? result + '.' + decPart : result;
}
var baseMem = system.getFreeMem();

const clock = new Label();
windows.addWindow(clock);
let idx = 0;
let oldWidth = 0;
let oldHeight = 0;
setInterval(() => {
  idx++;
  const free = system.getFreeMem();
  clock.title = `mem used: ${format(baseMem - free)}, free: ${format(free)}, ${idx}`;
  clock.left = 1024 - clock.width - 10;
  clock.top = 768 - clock.height - 10;
  clock.refresh();
  // clock.afterRender = ()=>{
  //   console.log("clock.afterRender");
  // };
  if(oldWidth != clock.width || oldHeight != clock.height){
    windows.refresh();
  }
  oldWidth = clock.width;
  oldHeight = clock.height;
}, 1000);

const menuList = [];//{title:string, onClick: ()=>void}
const menuForm = { form: null };
//start menu
{
  const button = new Button();
  button.title = "start";
  button.left = 10;
  button.top = 768 - button.height - 10;
  button.onClick = () => {
    console.log("click");
    if (menuForm.form) {
      windows.removeWindow(menuForm.form);
      menuForm.form = null;
      windows.refresh();
      return;
    } else {
      menuForm.form = new Form();
      let form = menuForm.form;
      form.title = "start menu";
      form.width = 300;
      form.height = 400;
      form.left = 10;
      form.top = button.top - form.height - 10;
      let startx = 10;
      let starty = 35;
      for (let item of menuList) {
        const btn = new Button();
        btn.title = item.title;
        if (startx + btn.width + 10 > form.width) {
          startx = 10;
          starty += btn.height + 10;
        }
        btn.left = startx;
        btn.top = starty;
        startx += btn.width + 10;
        form.addChild(btn);
        btn.onClick = () => {
          item?.onClick();
          windows.removeWindow(menuForm.form);
          menuForm.form = null;
          windows.refresh();
        }
      }
      windows.addWindow(form);
      form.onClose = () => {
        windows.removeWindow(menuForm.form);
        menuForm.form = null;
        windows.refresh();
      };
    }
  };
  windows.addWindow(button);
  // setTimeout(() => {
  //   onClick();
  // }, 1000);
}





let formId = 0;

const test1 = ()=>{
  let form = new Form();
  let id = formId+"";
  formId++;
  form.title = "form"+id;
  form.left = 478;
  form.top = 268;
  form.width = 300;
  form.height = 400;
  const btn = new Button();
  {
    btn.left = 10;
    btn.top = 30;
    btn.title = "fdsfd你好safdsa";
    form.addChild(btn);
  }
  const inp = new Input();
  {
    inp.left = 10;
    inp.top = 70;
    inp.width = 100;
    inp.height = 200;
    form.addChild(inp);
    form.setFocus(inp);
  }
  windows.addWindow(form);
  form.onClose = ()=>{
    windows.removeWindow(form);
    //form.removeChild(btn);
    //form.removeChild(inp);
    form = null;
    windows.refresh();
  };
};
menuList.push({title:"test1", onClick: test1});
// setTimeout(() => {
//   test1();
// }, 1000);


const test2 = ()=>{
  let form = new Form();
  let id = formId + "";
  formId++;
  form.title = "form" + id;
  form.left = 10;
  form.top = 10;
  form.width = 220;
  form.height = 140;

  {
    const btn = new Button();
    btn.left = 10;
    btn.top = 30;
    btn.title = "load";
    form.addChild(btn);
    
    btn.onClick = ()=>{
      form.img = util.loadImg("/mineswep.res/mc.bmp");
      form.jpg = util.loadImg("/jpg/test0.jpg");
      console.log("img", form.img.width, form.img.height);
      console.log("jpg", form.jpg.width, form.jpg.height);
      
      form.width = form.jpg.width + 20;
      form.height = form.jpg.height + 40;
   
      form.removeChild(btn);
    };
  }

  let i = 0;
  form.afterRender = () => {
    form.paint(10, 35, form.jpg.width, form.jpg.height, form.jpg.buffer);
    form.paint(50, 50, form.img.width, form.img.height, form.img.buffer);
    console.log(i);
    i++;
  };

  form.onClose = () => {
    windows.removeWindow(form);
    form = null;
    windows.refresh();
  };

  //forms[id] = form;
  windows.addWindow(form);
};
menuList.push({title:"test2", onClick: test2});
// setTimeout(() => {
//   test2();
// }, 1000);


const load3Dobj = (b3o, tex)=>{
  const info = fs.info(b3o);
  const stream = fs.open(b3o);

  const buf = new ArrayBuffer(info.size);
  stream.read(buf, info.size);
  
  const img = util.loadImg(tex);
  const obj = new Object3d(buf, img.buffer);

  return obj;
};
const test3d = ()=>{
  let form = new Form();
  let id = formId + "";
  formId++;
  form.title = "form" + id;
  form.left = 10;
  form.top = 10;
  form.width = 640 + 20;
  form.height = 480 + 80;

  let pos;
  let rot;
  let rot2;
  pos = 0;
  rot = 0;
  rot2 = 0;


  // form.paint(50, 50, buff1.width, buff1.height, buff1.buffer);
  // system.halt();

  {
    const btn = new Button();
    btn.left = 10;
    btn.top = 30;
    btn.title = "test";
    form.addChild(btn);

    const label = new Label();
    label.left = 60;
    label.top = 30;
    label.title = "                     ";
    form.addChild(label);

    let cube;
    let aaa;
    let sphere;
    btn.onClick = ()=>{
      
      if(!form.canvas3d){

        form.canvas3d = new Canvas3d(640, 480);//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        label.print("load cube");
        cube   = load3Dobj("/3dobj/cube.b3o"  , "/3dobj/cube.jpg"  );//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        label.print("load aaa");
        aaa    = load3Dobj("/3dobj/aaa.b3o"   , "/3dobj/aaa.jpg"   );//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        label.print("load sphere");
        sphere = load3Dobj("/3dobj/sphere.b3o", "/3dobj/sphere.jpg");//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        label.print("done");
      
        form.canvas3d.addObj(cube  );
        form.canvas3d.addObj(aaa   );
        form.canvas3d.addObj(sphere);
      
      }

      pos = pos + 0.5;
      //if (pos > 50) pos = -50;

      rot2 = rot2 + 0.03;
      //if (rot2 > 3.1416*2) rot2 = 0;

      cube.resetMatrix();
      cube.scale(1.0, 1.0, 1.0);
      cube.move(pos, 2.1, 2.1);
      
      aaa.resetMatrix();
      aaa.scale(0.2, 0.2, 0.2);
      //aaa.matrix.scale(0.5, 0.5, 0.5);
      //aaa.matrix.rotateY(rot);

      sphere.resetMatrix();
      sphere.scale(2.0, 2.0, 2.0);
      sphere.rotateY(-rot2);
      sphere.move(3.9, 0, 0);
      sphere.rotateY(rot2);
      
      console.log("before render");
      form.buff1 = form.canvas3d.render();//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      console.log("after render");
      form.refresh();
    };
  }

  let i = 0;
  form.afterRender = () => {
    //console.log("form.afterRender");
    //form.paint(10, 25, 256, 256, (i&1) ? 0xFFFF0000 : 0x0000FFFF);
    //img.width 
    form.buff1 && form.paint(10, 70, form.buff1.width, form.buff1.height, form.buff1.buffer);
    console.log(i);
    i++;
  };

  form.onClose = () => {
    windows.removeWindow(form);
    form = null;
    windows.refresh();
  };

  //forms[id] = form;
  windows.addWindow(form);

};
menuList.push({title:"test3d", onClick: test3d});
// setTimeout(() => {
//  test3d();
// }, 2000);


// setTimeout(()=>{
//   form = new Form();
//   form.title = "fdsa";
//   form.left = 310;
//   form.top = 468;
//   form.width = 300;
//   form.height = 200;
//   windows.addWindow(form);
// }, 1000);
// setTimeout(()=>{
//   console.log("onClose start");
//   windows.removeWindow(form);
//   form = null;
//   windows.refresh();
//   console.log("onClose end");
// }, 2000);


// let window = new Window({width:200,height:100,left:300,top:500});
// window.onMouseDown = (x,y,e)=>{console.log("---onMouseDown---",x,y,e);};
// window.onMouseUp = (x,y,e)=>{console.log("---onMouseUp---",x,y,e);};
// window.onMouseMove = (x,y,e)=>{console.log("---onMouseMove---",x,y,e);};
// window.onKeyEvent = (key,char)=>{console.log("---onKeyEvent---",key,char);};
// window.afterRender = ()=>{
//   console.log("window.afterRender");
// };
// windows.addWindow(window);


//
//      let ff;
//try {
//  const a = new Animal("Mystery");
//  //ff = a;
//  a.speak();
//} catch (e) {
//  console.log(e.message);
//}
//
//const d = new Dog("Lucky", 256);
//d.speak();              // => Dog Lucky says: Woof! (buffer size: 256)
//d.bufferSize = 123;
//console.log(d.bufferSize); // => 256
//
//
//


// var i = 1;
// setInterval(()=>{
// console.log("fdsa:"+i);
// i++;
// if(i>10){
//   //window = null;
// }
// },1000);

















// var img;
// var jpg;
// if(true){

//   //var ret = fs.list("/");
//   //var ret = fs.list("/jpg");
//   //console.log("ret",JSON.stringify(ret, 0, 2));

  
//   //console.log("img", img.width, img.height);

//   //system.halt();
//   img = util.loadImg("/mineswep.res/mc.bmp");
//   jpg = util.loadImg("/jpg/test0.jpg");
//   console.log("img", img.width, img.height);
//   console.log("jpg", jpg.width, jpg.height);
//   if(false){
//     let buf = new ArrayBuffer(256*256*2);
//     let arr = new Uint16Array(buf);
//     for(var i=0;i<256;i++){
//       //for(var j=0;j<16;j++){
//         arr[i*256+i] = util.getRGB565(0xFF0000);//util.getRGB565(0, i, j);
//         //console.log(arr[i*256+j]);
//       //}
//     }
//     system.flushCache();
//   }
  
//   // let stream = fs.open("/test/test11.txt");

//   // let buf = new ArrayBuffer(32);
//   // let arr = new Int8Array(buf);
//   // var size = stream.read(buf, 4000);

//   // console.log("size", size);

//   // //var arr = [];
//   // if(size){
//   //   for(var i=0;i<size;i++){
//   //     console.log(arr[i]);
//   //   }
//   // }
// }
// // let buf = new ArrayBuffer(32);
// // let arr = new Int32Array(buf);
// // arr[1] = 123;
// // arr[7] = 22;
// // console.info(buf);
// // console.info(arr);

// // let arr = new Uint32Array(5000);
// // //let arr = new Uint8Array(5000);
// // arr[2] = 123;
// // console.demoproc(arr);
